package com.future.biz;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.future.dao.InforShareAnswerDAO;
import com.future.dao.ShareTotleDAO;
import com.future.model.bo.ShareTotleGridBO;
import com.future.model.bo.UserBO;

public class ShareTotleGrid implements GenerateGrid<ShareTotleGridBO> {

	@Override
	public Map<Integer, ShareTotleGridBO> generateGrid(UserBO userBo) {
		Integer areaId = userBo.getArea().getId();
		Map<Integer, ShareTotleGridBO> result = new HashMap<Integer, ShareTotleGridBO>();
		InforShareAnswerDAO inforShareAnswerDao = new InforShareAnswerDAO();
		List<Map<String, Object>> countResult = inforShareAnswerDao.count(areaId);
		Iterator<Map<String, Object>> countIter = countResult.iterator();
		while (countIter.hasNext()) {
			Map<String, Object> mm = countIter.next();
			ShareTotleGridBO stgBo = new ShareTotleGridBO();
			stgBo.setShareDepartmentName(mm.get("department").toString());
			stgBo.setInforid((Integer) mm.get("inforid"));
			stgBo.setShareDepartmentId((Integer) mm.get("deptid"));
			if (mm.get("shareTotle") == null) {
				stgBo.setTotle(0);
			} else {
				stgBo.setTotle(((BigDecimal) mm.get("shareTotle")).intValue());
			}
			result.put((Integer) mm.get("inforid"), stgBo);
		}
		return result;
	}

	@Override
	public List<ShareTotleGridBO> loadGrid(UserBO userBo) {
		ShareTotleDAO shareTotleDao = new ShareTotleDAO();
		List<ShareTotleGridBO> result = new ArrayList<ShareTotleGridBO>();
		ShareTotleGridBO stgBo = null;
		List<Map<String, Object>> countResult = shareTotleDao.countByUserId(userBo.getUser().getId());
		Iterator<Map<String, Object>> countIter = countResult.iterator();
		while (countIter.hasNext()) {
			Map<String, Object> mm = countIter.next();
			stgBo = new ShareTotleGridBO();
			stgBo.setInforid((Integer) mm.get("inforid"));
			stgBo.setInformation((String) mm.get("information"));
			stgBo.setShareDepartmentId((Integer) mm.get("deptid"));
			stgBo.setShareDepartmentName((String) mm.get("department"));
			stgBo.setTotle((Integer) mm.get("totle"));
			result.add(stgBo);
		}

		return result;
	}

}
